package com.itheima.exerciseTest.method30_39.method33;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.Random;

public class ServerDemo {
    public static void main(String[] args) throws IOException {
        Random r = new Random();
        while (true) {
            int port = r.nextInt(1000) + 10000;
            int i1 = r.nextInt(256);
            int i2 = r.nextInt(256);
            int i3 = r.nextInt(256);
            InetAddress byName = InetAddress.getByName("224." + i1 + "." + i2 + "." + i3);

            MulticastSocket ms = new MulticastSocket(port);
            ms.joinGroup(byName);

            byte[] bytes = new byte[1024];
            DatagramPacket dp = new DatagramPacket(bytes,1024);

            ms.receive(dp);

            System.out.println(new String(bytes,0,dp.getLength()));

        }
    }
}
